from selenium.webdriver import ActionChains
from base import webdriver, By

userinfo = {'account': '123456@qq.com', 'password': '123456'}


class QqEmailLogin:
    def __init__(self):
        self.driver = webdriver.Chrome()
        self.driver.get('https://mail.qq.com/')
        self.driver.maximize_window()

    def qqlogin(self):
        # 定位QQ登录方式的位置,并点击
        self.driver.find_element(By.ID, 'qqLoginTab').click()
        # 切换登录框架
        self.driver.switch_to.frame('login_frame')
        # 定位邮箱账号输入框,并清空上一次登录的账号
        self.driver.find_element(By.ID, 'u').clear()
        # 输入邮箱账号
        self.driver.find_element(By.ID, 'u').send_keys(userinfo['account'])
        # 定位密码输入框,输入密码
        self.driver.find_element(By.ID, 'p').send_keys(userinfo['password'])
        # 定位授权登录按钮,并点击
        self.driver.find_element(By.ID, 'login_button').click()

    def verify(self):  # 图形验证阶段
        # 定位图形验证滑块位置
        source = self.driver.find_element(By.ID, 'tcaptcha_drag_button')
        # 创建鼠标事件
        ac = ActionChains(self.driver)
        # 按住鼠标
        ac.click_and_hold(source).perform()
        # 移动鼠标
        ac.move_by_offset(xoffset=177, yoffset=0).perform()
        # 释放鼠标
        ac.release().perform()

    def quit(self):
        self.driver.quit()


if __name__ == '__main__':
    qq_email_login = QqEmailLogin()
    qq_email_login.qqlogin()
    qq_email_login.verify()
    # qq_email_login.quit()